package com.yiibai.servlet;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.jdom.JDOMException;

import com.springmvc.annotation.Controller;
import com.springmvc.annotation.RequestMapping;
import com.springmvc.core.ParseSpringmvc;
import com.springmvc.core.ScanSubPackege;
import com.springmvc.entry.ComponentScan;
import com.springmvc.entry.ContextConfig;

public class MyContextListener implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("销毁了");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		//System.out.println("监听到了");
		try {
			ParseSpringmvc parseSpringmvc = new ParseSpringmvc();
			URL resource = parseSpringmvc.getClass().getClassLoader().getResource("springmvc.xml");
			String path = resource.getPath();
			ContextConfig parseConfig = parseSpringmvc.parseConfig(path);
			//System.out.println(parseConfig.getAnnotationDrivenValue());
			ServletContext servletContext = arg0.getServletContext();
			servletContext.setAttribute("contextConfig", parseConfig);
			ScanSubPackege scanSubPackege = new ScanSubPackege();
			List<ComponentScan> componentScanList = parseConfig.getComponentScanList();
			HashMap<String, Object> hashMap = new HashMap<>();
			HashMap<String, HashMap<String, String>> requestMap = new HashMap<>();
			if(componentScanList.size()>0){
			for (ComponentScan componentScan : componentScanList) {
				String basePackage = componentScan.getBasePackage();
				ArrayList<String> scan = scanSubPackege.scan(basePackage);
				
				//包名和多个类名
				if(scan.size()>0){
					for (String string : scan) {
						String name= basePackage + "." + string;
						System.out.println(name);
						Class<?> forName = Class.forName(name);
						Object newInstance = forName.newInstance();
						hashMap.put(name, newInstance);
						//解析类中的request注解,路径和方法
						RequestMapping annotation = forName.getAnnotation(RequestMapping.class);
						if(annotation!=null){
							String value = annotation.value();
							if(value!=null&&value!=""){
								Method[] methods = forName.getMethods();
								if(methods.length>0){
									for (Method method : methods) {
										if(method.isAnnotationPresent(RequestMapping.class)){
										RequestMapping annotation2 = method.getAnnotation(RequestMapping.class);
										String value2 = annotation2.value();
										//System.out.println(value2);
										if(value2!=null&&value2!=""){
											String requestValue = value+value2;
											String methodName = method.getName();
											//System.out.println(requestValue);
											HashMap<String, String> hashMap2 = new HashMap<String,String>();
											hashMap2.put(name, methodName);
											requestMap.put(requestValue, hashMap2);
										}
										}
									}
								}
							}
						}
					}
					
				}
			}
			}
			servletContext.setAttribute("beanList", hashMap);
			servletContext.setAttribute("requestMap", requestMap);
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

}
